home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / inter54e.zip / INTSUM16.ZIP / CONFIG.CPP next >
C/C++ Source or Header  |  1996-10-13  |  9KB  |  247 lines

  1. //********************************************************************
  2. //                                                                    
  3. //  CONFIG.CPP -  INI-file handling routines                          
  4. //                                                                    
  5. //  Copyright (c) 1996 Daniel D. Miller                               
  6. //                                                                    
  7. //  Last Update:  10-14-95 11:37am                                    
  8. //                                                                    
  9. //  Compile with makefile                                             
  10. //                                                                    
  11. //********************************************************************
  12. #include <stdio.h>
  13. #include <stdlib.h>     //  strtol(), _splitpath(), _makepath()
  14. #include <string.h>
  15. #include "intsum.hpp"
  16. #include "err_exit.hpp"
  17. // #include <memcheck.h>
  18.  
  19. static char parse_text[81] ;
  20.  
  21. //********************************************************************
  22. //  .INI-file strings
  23. //  Note:  INI groups will not be implemented in this application
  24. //********************************************************************
  25.  
  26. uchar BORDER    = 0x06 ;
  27. uchar LOGO      = 0x13 ;
  28. uchar MAIN_TEXT = 0x02 ;
  29. uchar SELECTED  = 0x31 ;
  30. uchar FILENAME  = 0x1B ;
  31. uchar MESSAGE   = 0x1C ;
  32. uchar INPUT     = 0x34 ;
  33. uchar HELP_TEXT = 0x07 ;
  34.  
  35. struct config_opt {
  36.    char* name ;
  37.    uchar* attr ;
  38.    } ;
  39.  
  40. //  entries in [text]
  41. config_opt text_group[] = {
  42. { "border", &BORDER },
  43. { "logo", &LOGO },
  44. { "main_text", &MAIN_TEXT },
  45. { "selected", &SELECTED },
  46. { "filename", &FILENAME },
  47. { "message", &MESSAGE },
  48. { "input", &INPUT },
  49. { "help_text", &HELP_TEXT },
  50. { "video_seg", NULL },
  51. { "list_dir", NULL },
  52. { NULL, NULL } } ;
  53.  
  54. //***************  function prototypes  ***************
  55. static int  write_ini_values(char* ini_name);
  56. static int  init_config(char* ini_name);
  57. static void parse_line(char* tptr);
  58.  
  59. //**************************************************************
  60. //  If INTSUM.INI exists in the appropriate directory,          
  61. //  read it.  Otherwise, write a default file in that place.    
  62. //**************************************************************
  63. int read_ini_file(void)
  64.    {
  65.    char ini_name[128] ; //  
  66.    int result = DATA_OKAY ;
  67.  
  68.    _makepath (ini_name, inidrive, inipath, "intsum", ".INI") ;
  69.  
  70.    //****************************************************
  71.    //  now, we believe we have determined an INI name.   
  72.    //  Try to open the file, and abort if it doesn't     
  73.    //  exist.                                            
  74.    //****************************************************
  75.    FILE* initest = fopen(ini_name, "rt") ;
  76.    if (initest == NULL)
  77.       {
  78.       //************************************************
  79.       //  intsum.INI does not already exist;
  80.       //  write the default file.
  81.       //************************************************
  82.       result = write_ini_values(ini_name) ;
  83.       }
  84.    else
  85.       {
  86.       //************************************************
  87.       //  intsum.INI already exists
  88.       //************************************************
  89.       fclose(initest) ;   //  file is there - continue
  90.       result = init_config(ini_name) ;
  91.       }
  92.    return result ;
  93.    }
  94.  
  95. //**************************************************************
  96. static int write_ini_values(char* ini_name)
  97.    {
  98.    FILE* inifile = fopen(ini_name, "wt") ;
  99.    if (inifile == NULL)
  100.       return(NO_WRITE_FILE) ;
  101.  
  102. fprintf(inifile, "; INTSUM.INI - default configuration file for INTSUM.EXE\n") ;
  103. fprintf(inifile, "; All attributes must be in two-digit hex format, preceded by 0x,\n") ;
  104. fprintf(inifile, "; as shown below.  \n") ;
  105. fprintf(inifile, "; Comments are marked with a semicolon.  All data following a\n") ;
  106. fprintf(inifile, "; semicolon is ignored to end of line.\n") ;
  107. fprintf(inifile, "\n") ;
  108. fprintf(inifile, "BORDER       = 0x%02X ; window borders\n", BORDER) ;
  109. fprintf(inifile, "LOGO         = 0x%02X ; window banners\n", LOGO) ;
  110. fprintf(inifile, "MAIN_TEXT    = 0x%02X ; primary text color\n", MAIN_TEXT) ;
  111. fprintf(inifile, "SELECTED     = 0x%02X ; current cursor line\n", SELECTED) ;
  112. fprintf(inifile, "FILENAME     = 0x%02X ; filename in list window\n", FILENAME) ;
  113. fprintf(inifile, "MESSAGE      = 0x%02X ; status message color\n", MESSAGE) ;
  114. fprintf(inifile, "INPUT        = 0x%02X ; user input fields\n", INPUT) ;
  115. fprintf(inifile, "HELP_TEXT    = 0x%02X ; help-screen color\n", HELP_TEXT) ;
  116. fprintf(inifile, "\n") ;
  117. fprintf(inifile, "VIDEO_SEG    = 0x%04X ; Video segment: 0xB800 or 0xB000\n", 
  118.                   get_video_seg()) ;
  119. fprintf(inifile, "\n") ;
  120. fprintf(inifile, ";  Next line is path to Interrupt List files\n") ;
  121. fprintf(inifile, ";  NOTE:  This path MUST end with a backslash!!\n") ;
  122. fprintf(inifile, "LIST_DIR     = %s%s ; path to Interrupt List files\n", 
  123.                      ildrive,ilpath) ;
  124.  
  125.    fclose(inifile) ;
  126.    return END_OF_FILE ;
  127.    }
  128.  
  129. //**************************************************************
  130. static int init_config(char* ini_name)
  131.    {
  132.    if (ini_name == NULL)
  133.       error_exit(NO_INI_FILE, NULL) ;
  134.       
  135.    FILE* inifile = fopen(ini_name, "rt") ;
  136.    if (inifile == NULL)
  137.       error_exit(NO_INI_FILE, ini_name) ;
  138.  
  139.    //****************************************************
  140.    //  Each subsequent call to parse_ini will return one 
  141.    //  line from the file, or END_OF_FILE when finished. 
  142.    //****************************************************
  143.    while (fgets(parse_text, 80, inifile) != NULL)
  144.       {
  145.       int len = strlen(parse_text) ;
  146.       if (parse_text[len-1] == 0x0D  ||  parse_text[len-1] == 0x0A) 
  147.          parse_text[len-1] = NULL ; //  strip off newline char
  148.       parse_line(parse_text) ;   //  remove comments, spaces, tabs
  149.  
  150.       if (strlen(parse_text) > 0)  //  get next line (skip blank line)
  151.          {
  152.          //***********************************************
  153.          //  separate option name and value
  154.          //***********************************************
  155.          char* strptr = strchr(parse_text, '=') ;
  156.          if (strptr == NULL)
  157.             return BAD_FORMAT ;
  158.          *strptr++ = NULL ;
  159.  
  160.          //***********************************************
  161.          //  search for optname in option-name list
  162.          //***********************************************
  163.          config_opt *textptr = &text_group[0] ;
  164.          int j = 0 ;
  165.          int done = 0 ;
  166.          while (!done) 
  167.             {
  168.             //  if at end of config-options list, 
  169.             //  go to next line from input file.
  170.             //  This implies an unknown option, which we just ignore
  171.             if (textptr->name == NULL)
  172.                done = 1 ;
  173.  
  174.             //  if matching line is found, process it
  175.             else if (strcmp(textptr->name, parse_text) == 0)
  176.                {
  177.                //  if attribute is NULL, this is not a color item.
  178.                if (textptr->attr == NULL) 
  179.                   {
  180.                   //  if current element is video_seg, 
  181.                   //  call the setting function as well.
  182.                   if (strcmp(textptr->name, "video_seg") == 0)
  183.                      {
  184.                      unsigned vseg = (unsigned) strtol(strptr, NULL, 0) ;
  185.                      select_video_seg(vseg) ;
  186.                      }
  187.                   else 
  188.                      {
  189.                      // strcpy(int_list_dir, strptr) ;
  190.                      _splitpath(strptr, ildrive, ilpath, NULL, NULL) ;
  191.                      }
  192.                   }
  193.  
  194.                //  if attrib is NOT NULL, it's a color item
  195.                else 
  196.                   {
  197.                   *(textptr->attr) = (uchar) strtol(strptr, NULL, 0) ;
  198.                   }
  199.                done = 1 ;
  200.                }
  201.  
  202.             //  if matching line not yet found, try next line item
  203.             else 
  204.                {
  205.                textptr = &text_group[++j] ;
  206.                }
  207.             }
  208.          }  //  if current line is not blank after stripping comments
  209.       }  //  while looping thru ini-file
  210.    fclose(inifile) ;
  211.    return DATA_OKAY ;
  212.    }
  213.  
  214. //*********************************************************************
  215. //  This routine parses the string pointed to by (tptr).               
  216. //  The following tasks are performed:                                 
  217. //                                                                     
  218. //     - remove everything from the first semicolon.                   
  219. //     - remove all spaces and tabs between SOL and EOL.               
  220. //     - convert remaining line to lower case.                         
  221. //                                                                     
  222. //*********************************************************************
  223. static void parse_line(char* tptr)
  224.    {
  225.    int head=0, tail=0, done=0 ;
  226.    do
  227.       {
  228.       if ((*(tptr+tail) == ';') || (*(tptr+tail) == NULL))
  229.          {
  230.          *(tptr+head) = NULL ;
  231.          done = 1 ;
  232.          }
  233.       else if ((*(tptr+tail) == ' ') || (*(tptr+tail) == 0x09 ))
  234.          tail++ ;
  235.       else
  236.          {
  237.          if (tail > head)
  238.             *(tptr+head) = *(tptr+tail) ;
  239.          head++ ;
  240.          tail++ ;
  241.          }
  242.       }
  243.    while (!done);
  244.    strlwr(tptr) ;
  245.    }
  246.  
  247.